package com.zjw.designPattern.proxy;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;

/**
 * 想实现一个行驶的功能，车只实现行驶，让代理来计算行驶了多久
 * Moveable 接口是要做的具体事情
 * Car 具体实现接口内容的对象
 * moveableProxy是委托对象创建出的代理对象
 * */

public class Car implements Moveable {
    private static final Logger logger = LoggerFactory.getLogger(Car.class);

    @Override
    public void move() throws InterruptedException {
        Thread.sleep(1000);

        logger.info("行驶...");
    }

    public static void main(String[] args) throws InterruptedException {
        //委托对象(被代理对象)
        Car car = new Car();
        InvocationHandler handler = new TimeHandler(car);
        //被创造出的代理对象
        Moveable moveableProxy = (Moveable) Proxy.newProxyInstance(car.getClass().getClassLoader(),car.getClass().getInterfaces(),handler);
        moveableProxy.move();
    }
}
